/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package inet.dao;

import inet.entity.TimeKeeping;
import inet.entity.TimeKeepingDetail;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ÂuNgọcThái
 */
public class TimeKeepingDetailDAO extends BaseDAO{
    public List<TimeKeepingDetail> getAllTKD(BigDecimal id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        List<TimeKeepingDetail> lstTKD = null;
        try {
            sql = "SELECT TKD_ID,TIMEKEEPING,TK_ID,GEN_DATE,LAST_UPDATE FROM TIMEKEEPINGDETAIL WHERE TK_ID IN (?)";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, id);
            rs = ps.executeQuery();
            TimeKeepingDetail tkd = null;
            while (rs.next()) {
                tkd = new TimeKeepingDetail();
                if (lstTKD == null) {
                    lstTKD = new ArrayList<TimeKeepingDetail>();
                }
                int i = 1;
                tkd.setTkd_id(rs.getBigDecimal(i++));
                tkd.setTimekeeping(rs.getString(i++));
                tkd.setTk_id(rs.getBigDecimal(i++));
                tkd.setGen_date(rs.getString(i++));
                tkd.setLast_update(rs.getString(i++));
                lstTKD.add(tkd);
            }
            return lstTKD;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }
    public int countTKD(BigDecimal id){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            String sql = "SELECT COUNT(*) FROM TIMEKEEPINGDETAIL WHERE TK_ID=?";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, id);
            rs = ps.executeQuery();
            int i = 1;
            if(rs.next()) return rs.getInt(1);
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            releaseConnection(conn, ps, rs);
        }
        return 0;
    }
//    public static void main(String[] a){
//        TimeKeepingDetailDAO d= new TimeKeepingDetailDAO();
//        if(d.getAllTKD(BigDecimal.valueOf(1))==null){
//            System.out.print("loi");
//        }else{
//            System.out.print("ok");
//        }
//        
//    }
    public  List<TimeKeepingDetail> getAll(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        List<TimeKeepingDetail> lstTKD = null;
        try {
            sql = "SELECT TKD_ID, TIMEKEEPING,TK_ID,GEN_DATE,LAST_UPDATE FROM TIMEKEEPINGDETAIL WHERE TK_ID IN(SELECT TK_ID FROM TIMEKEEPING WHERE TK_NAME ='03/2015' );";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            TimeKeepingDetail tkd = null;
            while (rs.next()) {
                tkd = new TimeKeepingDetail();
                if (lstTKD == null) {
                    lstTKD = new ArrayList<TimeKeepingDetail>();
                }
                int i = 1;
                tkd.setTkd_id(rs.getBigDecimal(i++));
                tkd.setTimekeeping(rs.getString(i++));
                tkd.setTk_id(rs.getBigDecimal(i++));
                tkd.setGen_date(rs.getString(i++));
                tkd.setLast_update(rs.getString(i++));
                lstTKD.add(tkd);
            }
            return lstTKD;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }
    
}
